//
//  LiveNewSignView.m
//  AFNetworking
//
//  Created by tianlong on 2019/8/13.
//

#import "LiveNewSignView.h"
#import "Masonry.h"

@interface LiveNewSignView ()
@property (weak,   nonatomic) UIView *bgView;
@property (weak,   nonatomic) UIImageView *skinView;
@property (weak,   nonatomic) UILabel *stuNameLabel;
@property (weak,   nonatomic) UILabel *descLabel;
@property (weak,   nonatomic) UIButton *closeButton;
@property (weak,   nonatomic) UIImageView *resultView;
@property (weak,   nonatomic) UILabel *resultLabel;
@property (weak,   nonatomic) UIButton *signButton;
@end


@implementation LiveNewSignView

- (instancetype)initWithStuName:(NSString *)stuName
                      stuAvatar:(NSString *)stuAvatar {
    self = [super initWithStuName:stuName stuAvatar:stuAvatar];
    if (self) {
        [self initSubViews];
        [self setupLayout];
    }
    return self;
}

- (void)initSubViews {
    
    CGFloat scale = [self referScale];
    
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor colorWithHexValue:0x000000 alpha:0.6];
    [self addSubview:bgView];
    
    UIImageView *skinView = [[UIImageView alloc] init];
    [self addSubview:skinView];
    self.skinView = skinView;
    
    UILabel *stuNameLabel = [[UILabel alloc] init];
    stuNameLabel.textAlignment = NSTextAlignmentCenter;
    stuNameLabel.text = [NSString stringWithFormat:@"%@你好",self.stuName];
    [self addSubview:stuNameLabel];
    self.stuNameLabel = stuNameLabel;
    
    UILabel *descLabel = [[UILabel alloc] init];
    descLabel.text = @"现在开始课前点名";
    descLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:descLabel];
    self.descLabel = descLabel;
    
    UIButton *signButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [signButton addTarget:self action:@selector(clickedSignButton:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:signButton];
    self.signButton = signButton;
    
    UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [closeButton addTarget:self action:@selector(clickedCloseButton:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:closeButton];
    self.closeButton = closeButton;
    BOOL isPrimary = YES;
    //弹框框架图
    if(isPrimary){
        skinView.image = [UIImage imageNamed:@"live_newsign_primaryschool_skin"];
    }else{
        skinView.image = [UIImage imageNamed:@"live_newsign_skin"];
    }
    //学生姓名
    stuNameLabel.font = [UIFont fontPingFangSCWithSize:14 *scale];
    //课前点名提示
    descLabel.font = [UIFont fontPingFangSCWithSize:14 *scale];
    if(isPrimary){
        stuNameLabel.textColor = [UIColor colorWithHexValue:0x5B4837];
        descLabel.textColor = [UIColor colorWithHexValue:0x5B4837];
    }
    else{
        stuNameLabel.textColor = [UIColor colorWithHexValue:0x212831];
        descLabel.textColor = [UIColor colorWithHexValue:0x212831];
    }
    
    //签到按钮
    CGSize size = CGSizeMake(72.0f*scale, 28.0f*scale);
    UIImage *nImage = [UIImage makeColorImage:[UIColor colorWithHexValue:0xEB002A alpha:1] imageSize:size];
     nImage = [UIImage makeRoundedImage:nImage cornerRadius:14.0*scale];
    
    UIImage *hImage = [UIImage makeColorImage:[UIColor colorWithHexValue:0xC50023 alpha:1] imageSize:size];
     hImage = [UIImage makeRoundedImage:hImage cornerRadius:14.0*scale];
    [signButton setBackgroundImage:nImage forState:UIControlStateNormal];
    [signButton setBackgroundImage:hImage forState:UIControlStateHighlighted];
    [signButton setTitle:@"签到" forState:UIControlStateNormal];
    [signButton setTitleColor:[UIColor colorWithHexValue:0xFFFFFF]  forState:UIControlStateNormal];
    [signButton setTitleColor:[UIColor colorWithHexValue:0xFFFFFF]  forState:UIControlStateHighlighted];
    signButton.titleLabel.font = [UIFont fontPingFangSCWithSize:14*scale];
    
    //关闭按钮
    [closeButton setBackgroundImage:[UIImage imageNamed:@"liveideo_uiclose_normal"] forState:UIControlStateNormal];
    [closeButton setBackgroundImage:[UIImage imageNamed:@"liveideo_uiclose_pressed"] forState:UIControlStateHighlighted];

}

#pragma mark - 布局
/** 布局签到面板 */
- (void)setupLayout {
    
    __weak typeof(self)selfWeak = self;
    //以667x375大小为参考标准，进行布局
    CGFloat scale = [self referScale];
    [self.bgView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.offset(0);
    }];
    
      CGFloat skinW = 270 *scale;
      CGFloat skinH = 250 *scale;
//      CGFloat skinTop = 57 *scale;
      CGFloat nameTop = 114 *scale;
      CGFloat descTop = 10 *scale;
      CGFloat signBtnBot = 41 *scale;
      CGFloat signBtnW = 72 *scale;
      CGFloat signBtnH = 28 *scale;
      CGFloat clsBtnW = 30 *scale;
      CGFloat clsBtnRight = 14 *scale;
      CGFloat clsBtnTop = 41 *scale;
       
       [self.skinView mas_remakeConstraints:^(MASConstraintMaker *make) {
           make.centerY.equalTo(selfWeak);
           make.centerX.equalTo(selfWeak);
           make.width.offset(skinW);
           make.height.offset(skinH);
       }];
    
    [self.stuNameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(selfWeak.skinView.mas_top).offset(nameTop);
        make.centerX.equalTo(selfWeak.skinView.mas_centerX);
    }];
    
    [self.descLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(selfWeak.stuNameLabel.mas_bottom).offset(descTop);
        make.centerX.equalTo(selfWeak.skinView.mas_centerX);
    }];
    
    [self.signButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(selfWeak.skinView.mas_bottom).offset(-signBtnBot);
        make.centerX.equalTo(selfWeak.skinView.mas_centerX);
        make.width.offset(signBtnW);
        make.height.offset(signBtnH);
    }];
    
    [self.closeButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(selfWeak.skinView.mas_right).offset(clsBtnRight);
        make.top.equalTo(selfWeak.skinView.mas_top).offset(clsBtnTop);
        make.width.offset(clsBtnW);
        make.height.offset(clsBtnW);
    }];
    
}

/** 布局结果控件 */
- (void)layoutResultViews {
    __weak typeof(self)selfWeak = self;
    CGFloat retViewTop;
    CGFloat retViewW;
    CGFloat retViewH;
    CGFloat retLabelBot;
    
    CGFloat scale = [self referScale];
    retViewTop = 114 *scale;
    retViewW = 60 *scale;
    retViewH = 60 *scale;
    retLabelBot = 45 *scale;
 
    [self.resultView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(selfWeak.skinView.mas_top).offset(retViewTop);
        make.centerX.equalTo(selfWeak.skinView);
        make.width.offset(retViewW);
        make.height.offset(retViewH);
    }];
    
    [self.resultLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(selfWeak.skinView.mas_bottom).offset(-retLabelBot);
        make.centerX.equalTo(selfWeak.resultView);
    }];
}



#pragma mark - 公开方法
- (void)showSignSuccess {
    [self hiddenSignSubViews:YES];
    if (self.skinType == LiveNewSkinTypePrimary) {
        self.resultView.image = [UIImage imageNamed:@"live_newsign_primaryschool_success"];
    } else {
        self.resultView.image = [UIImage imageNamed:@"live_newsign_success"];
    }
    
    self.resultLabel.text = @"签到成功";
    [self layoutResultViews];
}

- (void)showSignNot {
    [self hiddenSignSubViews:YES];

    if (self.skinType == LiveNewSkinTypePrimary) {
        self.resultView.image = [UIImage imageNamed:@"live_newsign_primaryschool_not"];
    } else {
        self.resultView.image = [UIImage imageNamed:@"live_newsign_not"];
    }
    self.resultLabel.text = @"未签到";
    [self layoutResultViews];
}

- (void)showSignMiss {
    [self hiddenSignSubViews:YES];

    if (self.skinType == LiveNewSkinTypePrimary) {
        self.resultView.image = [UIImage imageNamed:@"live_newsign_primaryschool_miss"];
    } else {
        self.resultView.image = [UIImage imageNamed:@"live_newsign_miss"];
    }
    self.resultLabel.text = @"错过签到时间";
    [self layoutResultViews];
}

#pragma mark - 私有方法
/** 隐藏签到面板中的一些控件 */
- (void)hiddenSignSubViews:(BOOL)hidden {
    self.stuNameLabel.hidden = hidden;
    self.descLabel.hidden = hidden;
    self.signButton.hidden = hidden;
    self.closeButton.hidden = hidden;
}

#pragma mark - getter and setter
- (UIImageView *)resultView {
    if (!_resultView) {
        UIImageView *view = [[UIImageView alloc] init];
        [self addSubview:view];
        _resultView = view;
    }
    return _resultView;
}

- (UILabel *)resultLabel {
    if (!_resultLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont fontPingFangSCWithSize:14 *[self referScale]];
        if (self.skinType == LiveNewSkinTypePrimary) {
            label.textColor = [UIColor colorWithHexValue:0x5B4837];
        } else {
            label.textColor = [UIColor colorWithHexValue:0x212831];
        }
        
        [self addSubview:label];
        _resultLabel = label;
    }
    return _resultLabel;
}

- (UIButton *)getSignButton {
    return self.signButton;
}

@end
